$(document).ready(function () {
    $('.dropdown-toggle').dropdown();

    // 以JSON方式获取表单数据
    function getJson(form) {
        var map = JSON.parse(JSON.stringify($(form).serializeArray()));
        var result = new Object();
        for(var i = 0; i < map.length; i++) {
            result[map[i].name] = map[i].value;
        }
        return result;
    }
    //条件查询
    $('#search_btn').on('click',function () {
        queryAjax();
    });

    //列表信息显示
    queryAjax();

    function queryAjax() {
        $.ajax({
            type: "POST",
            url: "appInfo/showAppInfo",
            data: getJson($('#search_form')[0]),
            dataType: "json",
            success: function (json) {
                appInfoList(json);
            }
        });
    }

    var $pageNo= $('.info_search input[name=pageNo]');
    var $pageTurning= $('.paging .pageTurning');
    $pageTurning.find(".first").on('click',function () {
        $pageNo.val(parseInt($pageNo.val())-1);
        queryAjax();
    });

    $pageTurning.find(".prev").on('click',function () {
        if( parseInt($pageNo.val()) > 1) {
            $pageNo.val(parseInt($pageNo.val())-1);
            queryAjax();
        }
    });

    $pageTurning.find(".next").on('click',function () {
        if( parseInt($pageNo.val()) < parseInt($('.pageCount').html())) {
            $pageNo.val(parseInt($pageNo.val())+1);
            queryAjax();
        }
    });

    $pageTurning.find(".last").on('click',function () {
        var pageCount= $('.pageCount').text();
        $pageNo.val(parseInt(pageCount));
        queryAjax();
    });

    function appInfoList(json) {
        $('.appInfo_list_body').html("");
        if( json ) {
            $(json.list).each(function (index) {
                $('.appInfo_list_body').append("<tr>\n" +
                "                                            <td>"+this.softwareName+"</td>\n" +
                "                                            <td>"+this.apkName+"</td>\n" +
                "                                            <td>"+this.softwareSize+"</td>\n" +
                "                                            <td>"+this.platform+"</td>\n" +
                "                                            <td>"+this.levelOne+"->"+this.levelTwo+"->"+this.levelThree+"</td>\n" +
                "                                            <td class='app_state' stateId='"+this.status+"'>"+this.stateName+"</td>\n" +
                "                                            <td>"+this.downloads+"</td>\n" +
                "                                            <td>"+this.newVersionName+"</td>\n" +
                "                                            <td>\n" +
                "                                                <div class=\"dropdown\">\n" +
                "                                                    <button type=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\" class=\"btn btn-info\">\n" +
                "                                                        点击操作\n" +
                "                                                        <span class=\"caret\"></span>\n" +
                "                                                    </button>\n" +
                "                                                    <ul appInfoId='"+this.id+"' class=\"dropdown-menu\" aria-labelledby=\"dLabel\">\n" +
                "                                                        <li class='lowerShelf' style='display:none;'>下架</li>\n" +
                "                                                        <li class='upperShelf' style='display:none;'>上架</li>\n" +
                "                                                        <li class='insertVersion'>新增版本</li>\n" +
                "                                                        <li class='modifyVersion'>修改版本</li>\n" +
                "                                                        <li class='modify'>修改</li>\n" +
                "                                                        <li class='query'>查看</li>\n" +
                "                                                        <li class='delete'>删除</li>\n" +
                "                                                    </ul>\n" +
                "                                                </div>\n" +
                "                                            </td>\n" +
                "                                        </tr>");

            });

            $('.dropdown .dropdown-menu li').addClass("dataManipulation");

            // 分页信息
            limitInfo(json);
                
            // 上架下架控制
            $('.appInfo_list_body .app_state').each(function () {
                var stateId= parseInt($(this).attr('stateId'));
                if( stateId == 4) {
                    $(this).parent('tr').find('.dropdown-menu').find('li').eq(0).show();
                }
                if( stateId == 5 ) {
                    $(this).parent('tr').find('.dropdown-menu').find('li').eq(1).show();
                }
            });

            // 绑定删除事件并发送删除请求
            var $dropdownMenu= $('.appInfo_list_body .dropdown-menu');
            var $lowerShelf= $dropdownMenu.find('.lowerShelf');
            var $upperShelf= $dropdownMenu.find('.upperShelf');
            var $insertVersion= $dropdownMenu.find('.insertVersion');
            var $modifyVersion= $dropdownMenu.find('.modifyVersion');
            var $modify= $dropdownMenu.find('.modify');
            var $query= $dropdownMenu.find('.query');

            //删除APP 数据信息
            var $delete= $dropdownMenu.find('.delete');
            deleteAppInfo($delete);
        }
    }

    function deleteAppInfo(element) {
        element.on('click',function () {
            var thisMy= $(this);
            var appInfoId= $(this).parent().attr('appInfoId');
            $.getJSON("appInfo/deleteAppInfo",{'id':appInfoId},function (json) {
                if( json==true ) {

                    // 清楚当前行
                    thisMy.parent().parent().parent().parent().remove();

                    // 总数据递减
                    $('.paging .totalCount').html(parseInt($('.paging .totalCount').html())-1)
                }
            });
        });
    }

    function limitInfo(json) {
        $('.paging .totalCount').html(json.totalCount);
        $('.paging .pageCount').html(json.totalPageCount);
        $('.paging .pageNo').html(json.currentPage);
    }
});



// function go(e,btn) {
//     var $reg= /^[1-9]\d*$/;
//     var text= e.value;
//     if(!$reg.test(text) || (text> pageCount)){
//         btn.css("cursor","not-allowed");
//         btn.attr('disabled',true);
//     }else {
//         btn.css("cursor","pointer");
//         btn.attr('disabled',false);
//     }
// }
